using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class MarketProductButton : MarketButton
{
	public Product product;

	protected MarketProductsPanel parentProductsPanel;

	public UIPanel buttonPanel;

	public MarketPriceTag priceTag;

	public UIButton infoButton;

	public UISprite infoButtonSprite;

	public UIButton upgradeButton;

	public GameObject blockObject;

	public UILabel blockLabel;

	public GameObject saleTag;

	public UILabel saleTagLabel;

	public GameObject newTag;

	public GameObject bestPriceTag;

	public UISprite buttonSprite;

	public Color selectedColor;

	public Color purchasedColor;

	public Color equippedColor;

	public Color normalColor;

	public float selectedScale = 1.02f;

	private bool availableTier = true;

	private List<MarketProductUpgradeIndicator> upgradeIndicatorList = new List<MarketProductUpgradeIndicator>();

	public MarketProductUpgradeIndicator upgradeIndicator;

	public UIGrid upgradesGrid;

	public List<Product> upgradeProducts = new List<Product>();

	private void OnDestroy()
	{
	}

	public override void OnClick()
	{
		if (product != null)
		{
			if (parentPanel.selected == id)
			{
				ShowInfoPanel();
				return;
			}
			base.OnClick();
			MarketControllerUI.instance.productsPanel.Select(id);
		}
	}

	public override void Init(string id, MarketPanel parentPanel)
	{
		base.Init(id, parentPanel);
		if (product is ProductVehicleUpgrade)
		{
			string text = product.id.Split('_')[0];
			text = text.Substring(0, text.Length - 1);
			text = text + "_" + (product as ProductVehicleUpgrade).targetType;
			if ((bool)title)
			{
				title.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Market/" + text + "_title"))) ? ScriptLocalization.Get("Market/" + text + "_title") : "Нет локализации");
			}
		}
	}

	public void InitProductButton(MarketProductsPanel parentPanel, Product product)
	{
		this.product = product;
		parentProductsPanel = parentPanel;
		this.product.ApplySale();
		Init(product.id, parentPanel);
		LoadProductTexture(product);
		availableTier = CupsManager.CurrentTier >= product.tier;
		League leagueForIndex = CupsManager.GetLeagueForIndex(product.tier - 1);
		if (!product.Purchased)
		{
			if (!availableTier)
			{
				blockLabel.text = string.Format(ScriptLocalization.Get("Market/itemLeagueRequired"), ScriptLocalization.Get("Game_Common/" + leagueForIndex));
			}
			blockObject.SetActive(!availableTier);
		}
		else
		{
			blockObject.SetActive(false);
		}
		if (string.IsNullOrEmpty(parentPanel.selected) && product.Equipped)
		{
			Select(true);
		}
		SetUpgrades();
	}

	public void ResetProductButtonInfo(bool resetUpgrades)
	{
		SetProductButtonInfo(parentPanel.selected.Equals(product.id));
		if (resetUpgrades)
		{
			SetUpgrades();
		}
	}

	public override void Select(bool selected)
	{
		if (selected)
		{
			if ((bool)parentPanel.selectedButton)
			{
				parentPanel.selectedButton.Select(false);
			}
			parentPanel.selected = product.id;
			parentPanel.selectedButton = this;
			parentProductsPanel.selectedProductButton = this;
			parentProductsPanel.selectedProduct = product;
			MarketController.KEY_SELECTED_VEHICLE = id;
			MarketControllerUI.instance.previewController.ShowPreview();
			parentProductsPanel.productMiniInfoPanel.ShowMiniInfoPanel();
		}
		else
		{
			if (infoButton != null)
			{
				infoButton.gameObject.SetActive(false);
			}
			if (upgradeButton != null)
			{
				upgradeButton.gameObject.SetActive(false);
			}
		}
		SetProductButtonInfo(selected);
	}

	private void SetProductButtonInfo(bool selected)
	{
		if (selected)
		{
			if (!(product is ProductSkin) && !(product is ProductBoost) && !(product is ProductVehicleUpgrade) && infoButton != null)
			{
				infoButton.gameObject.SetActive(true);
			}
			if (product is ProductCar && MarketController.Instance.HaveCarUpgrades(product.id) && product.Purchased && !MarketController.Instance.AllUpgradesAreBuyed(product.id))
			{
				if (infoButton != null)
				{
					infoButton.gameObject.SetActive(false);
				}
				if (upgradeButton != null)
				{
					upgradeButton.gameObject.SetActive(true);
				}
			}
			if (product is ProductComsumable)
			{
				parentProductsPanel.productMiniInfoPanel.ShowMiniInfoPanel();
			}
			MarketControllerUI.instance.mainButtons.SetMainButtons();
			buttonSprite.color = selectedColor;
			selectedBG.alpha = 1f;
			base.transform.localScale = Vector3.one * selectedScale;
			buttonPanel.depth = ((!(product is ProductVehicleUpgrade)) ? 135 : 165);
		}
		else
		{
			buttonSprite.color = normalColor;
			selectedBG.alpha = 0f;
			base.transform.localScale = Vector3.one;
			buttonPanel.depth = ((!(product is ProductVehicleUpgrade)) ? 130 : 160);
		}
		SetStateAndTags(selected);
	}

	private void SetStateAndTags(bool selected)
	{
		if (product == null)
		{
			return;
		}
		if (!product.Purchased)
		{
			if (product.availableForBuy)
			{
				priceTag.SetPrice(product);
				priceTag.gameObject.SetActive(true);
			}
			else
			{
				priceTag.gameObject.SetActive(false);
			}
			newTag.SetActive(product.newProduct);
			bestPriceTag.SetActive(product.bestPrice);
		}
		else
		{
			priceTag.gameObject.SetActive(false);
			newTag.SetActive(false);
			bestPriceTag.SetActive(false);
			buttonSprite.color = purchasedColor;
		}
		if (product.sale != null && product.SalePercent != 0f)
		{
			saleTag.SetActive(true);
			saleTagLabel.text = "-" + product.SalePercent + "%";
		}
		else
		{
			saleTag.SetActive(false);
		}
		if (product.Equipped)
		{
			parentProductsPanel.equippedProductButton = this;
			buttonSprite.color = equippedColor;
		}
	}

	private void SetUpgrades()
	{
		if (!this.product.Purchased && !(this.product is ProductVehicleUpgrade))
		{
			upgradesGrid.gameObject.SetActive(false);
			return;
		}
		upgradesGrid.gameObject.SetActive(true);
		for (int i = 0; i < upgradeIndicatorList.Count; i++)
		{
			Object.DestroyImmediate(upgradeIndicatorList[i].gameObject);
		}
		upgradeIndicatorList.Clear();
		upgradeProducts.Clear();
		upgradeIndicator.gameObject.SetActive(false);
		int num = 0;
		Product product = this.product;
		while (product.prevProduct != null)
		{
			product = product.prevProduct;
			num++;
		}
		if (this.product is ProductArmor)
		{
			switch (num % 3)
			{
			case 0:
				SetUpgradesFrom(this.product, 3);
				break;
			case 1:
				SetUpgradesFrom(this.product.prevProduct, 3);
				break;
			case 2:
				if (this.product.prevProduct != null)
				{
					SetUpgradesFrom(this.product.prevProduct.prevProduct, 3);
				}
				break;
			}
		}
		else
		{
			do
			{
				upgradeProducts.Add(product);
				InitUpgradeIndicator(product);
				product = product.nextProduct;
			}
			while (product != null);
		}
		upgradesGrid.enabled = true;
	}

	private void SetUpgradesFrom(Product fromProducrt, int count)
	{
		Product product = fromProducrt;
		for (int i = 0; i < count; i++)
		{
			if (product == null)
			{
				break;
			}
			upgradeProducts.Add(product);
			InitUpgradeIndicator(product);
			product = product.nextProduct;
		}
	}

	private void InitUpgradeIndicator(Product tmpProduct)
	{
		MarketProductUpgradeIndicator marketProductUpgradeIndicator = Object.Instantiate(upgradeIndicator);
		marketProductUpgradeIndicator.transform.SetParent(upgradesGrid.transform);
		marketProductUpgradeIndicator.transform.localScale = Vector3.one;
		marketProductUpgradeIndicator.transform.localPosition = Vector3.zero;
		marketProductUpgradeIndicator.gameObject.SetActive(true);
		marketProductUpgradeIndicator.Init(tmpProduct);
		upgradeIndicatorList.Add(marketProductUpgradeIndicator);
	}

	public void ShowInfoPanel()
	{
		if (!(product is ProductSkin) && !(product is ProductBoost) && !(product is ProductVehicleUpgrade))
		{
			parentProductsPanel.productInfoPanel.ShowInfoPanel();
		}
	}

	private void VisibilityChange(bool visible)
	{
	}
}
